package com.xigua.数组;

import java.util.*;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/1418:12
 */
public class _599_两个列表的最小索引总和 {

    public static void main(String[] args) {
        String [] list1 = new String[]{"Shogun","Tapioca Express","Burger King","KFC"};
        String [] list2 = new String[]{"KFC","Shogun","Burger King"};
        findRestaurant(list1,list2);
    }

    public static String[] findRestaurant(String[] list1, String[] list2) {
        List<String> ans = new ArrayList<>();
        HashMap<String,Integer> map = new HashMap<>();
        int minIndex = Integer.MAX_VALUE;
        for (int i = 0; i < list1.length; i++) {
            map.put(list1[i],i);
        }
        for (int i = 0; i < list2.length; i++) {
            String key = list2[i];
            if (map.containsKey(list2[i]) && (map.get(key)+i) < minIndex  ){
                minIndex = map.get(key)+i;
                ans.clear();
                ans.add(key);
            }else if (map.containsKey(list2[i]) && (map.get(key)+i) == minIndex ){
                ans.add(key);
            }
        }
        String [] result = new String[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            result[i] = ans.get(i);
        }
        return result;
    }



}
